var VK = VK || {};
VK.U = function (url) {
    var host = document.domain;
    var nowUrl = window.location.href;
    var protocol = window.location.protocol;
    var port = window.location.port;
    var host = protocol + '//' + host;
    if (port) {
        host = host + ':' + port;
    }
    host = host + '/';
    var urlStr = nowUrl.replace(host, '');
    if (urlStr.indexOf("?") != -1) {
        urlStr = urlStr.split("?")[0];
    }
    var urlArr = urlStr.split('/');

    if (urlArr) {
        if (urlArr[0] == 'manage' && url) {
            host = host + 'manage/';
        } else {
            host = host + 'manage'
        }
    }

    return host + url;
}

VK.QAjax = function (url, methord, param, successCallBack, errorCallBack) {
    $.ajax({
        url: url,
        type: methord,
        data: param,
        dataType: 'json',
        timeout: 15000,
        success: function (res) {
            if (res.status == 401) {
                VK.error('还未登录或登录已失效');
                setTimeout(function () {
                    if (window != top) {
                        top.location.href = VK.U('login');
                    } else {
                        window.location.href = VK.U('login');
                    }
                }, 1000);
                return false;
            } else if (res.status == 300) {
                VK.error('您的账号已在其他地点登录，请重新登录');
                setTimeout(function () {
                    if (window != top) {
                        top.location.href = VK.U('login');
                    } else {
                        window.location.href = VK.U('login');
                    }
                }, 1000);
                return false;
            } else if (res.status == 403) {
                layer.closeAll();
                VK.error(res.msg);
                return false;
            }
            successCallBack(res);
        },
        error: function (res) {
            if (typeof errorCallBack == "function") {
                errorCallBack(res);
            }
        }
    })
}

/**
 * 打开弹窗页面
 * @param title
 * @param url
 * @param isFull
 * @param width
 * @param height
 * @param endCallBack
 */
VK.openPoerWindow = function (title, url, isFull, width, height, endCallBack) {
    var windowWith = $(window).width();
    var windowHeight = $(window).height();
    if (isFull == 0) {
        var maxWidth = windowWith * 0.9;
        var maxHeight = windowHeight * 0.9;
        width = width > maxWidth ? maxWidth : width;
        height = height > maxHeight ? maxHeight : height;
    } else {
        width = windowWith;
        height = windowHeight;
    }
    layer.open({
        type: 2,
        title: title,
        //maxmin : true,
        area: [width + 'px', height + 'px'],
        content: url,
        resize: true,
        shade: 0.4,
        fix: false,
        end: function () {
            if (typeof endCallBack == 'function') {
                endCallBack();
            }
        }
    });
}

/**
 * alert框
 */
VK.alert = function (msg, callBack) {
    layer.alert(msg);
}

/**
 * 错误提示
 */
VK.error = function (msg, icon = 2) {
    layer.msg(msg, {icon: icon, time: 2000});
}

/**
 * 成功提示
 */
VK.success = function (msg) {
    layer.msg(msg, {icon: 1, time: 2000});
}

/**
 * 加载中提示
 */
VK.load = function () {
    return layer.load();
}

/**
 * 关闭加载中提示
 */
VK.closeLoad = function (handel) {
    layer.close(handel);
}

/**
 * 关闭当前页面
 */
VK.closeNowPage = function () {
    var mylay = parent.layer.getFrameIndex(window.name);
    parent.layer.close(mylay);
}

/**
 * 通知父页面
 * callback 执行的方法
 * params 参数值，参数顺序必须和callback定义的一致
 */
VK.nofifyParentPage = function (callback, params) {
    var parentsObj = parent;
    var paramsStr = '';
    if (typeof params == 'object') {
        for (var i = 0; i < params.length; i++) {
            paramsStr += ",'" + params[i] + "'"
        }
    }
    paramsStr = paramsStr.slice(1);
    funStr = "parent." + callback + '(' + paramsStr + ')';
    eval(funStr);
}

/**
 * 确认弹框
 * {content:"提示文字",yes:function(){}}
 */
VK.confirm = function (options) {

    var opts = {};
    opts = $.extend(opts, {title: '系统提示', offset: '100px'}, options);
    return layer.confirm(opts.content, {
        icon: 3,
        title: opts.title,
        offset: opts.offset,
        shift: 6,
    }, options.yes, options.cancel);
}
